package com.psg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.psg.dao.ProduceDao;
import com.psg.dao.RecordDao;
import com.psg.pojo.Look;
import com.psg.pojo.Produce;
import com.psg.service.ProduceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProduceServiceImpl implements ProduceService {

    @Autowired
    private ProduceDao produceDao;
    @Autowired
    private RecordDao recordDao;

    @Override
    public Produce selectProduce(Integer pId) {
        return produceDao.selectById(pId);
    }

    @Override
    public void recordProduce(Look look) {
        recordDao.insert(look);
    }

    @Override
    public PageInfo<Look> recordPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Look> looks = recordDao.selectLimit();
        return new PageInfo<>(looks);
    }

    @Override
    public List<Produce> findAll() {
        return produceDao.selectList(null);
    }

    @Override
    public List<Produce> findByCategory(Integer category) {
        QueryWrapper<Produce> wrapper=new QueryWrapper<>();
        wrapper.eq("category",category);
        return produceDao.selectList(wrapper);
    }

    @Override
    public List<Produce> classify(String classify) {
        QueryWrapper<Produce> wrapper = new QueryWrapper<>();
        wrapper.like("short_name",classify);
        return produceDao.selectList(wrapper);
    }

    @Override
    public List<Produce> searchProduces(String searchText) {
        return produceDao.selectProducesLikeName("%"+searchText+"%");
    }

}
